<!--
 * @Author: zzr
 * @Date: 2024-12-03 17:18:42
 * @LastEditors: zzr
 * @Description: 
-->
<template>
  <div style="height: 100%">
    <div class="page-video">
      <div class="inner custom-scroll">
        <div class="left-box">
          <div class="page-title">
            <img src="/images/video/video-space-title.png" />
          </div>
          <div class="video-params">
            <el-form :model="params" label-width="80px" label-position="left">
              <el-tabs
                v-model="activeName"
                class="title-tabs"
                :stretch="true"
                @tabChange="tabChange"
              >
                <el-tab-pane label="" name="text2video">
                  <template #label>
                    <span class="tabs-label">
                      <el-icon>
                        <ChatLineSquare />
                      </el-icon>
                      <span>文生视频</span>
                    </span>
                  </template>
                  <div class="video-box">
                    <div class="param-line pt">
                      <div class="title">
                        <img
                          class="title-icon"
                          src="/images/video/creativity.svg"
                        />
                        <span class="title-text">创意描述：</span>
                      </div>
                    </div>
                    <div class="param-line">
                      <el-input
                        v-model="params.prompt"
                        :autosize="{ minRows: 6, maxRows: 10 }"
                        type="textarea"
                        ref="promptRef"
                        placeholder="请输入要生成的内容"
                      />
                    </div>
                  </div>
                  <div class="video-box">
                    <div class="param-line" style="padding-top: 10px">
                      <el-row class="video-slider">
                        <el-col :span="5">
                          <div class="video-slider-title text-left">
                            创意想象力
                          </div>
                        </el-col>
                        <el-col :span="14">
                          <el-slider
                            v-model.number="params.chaos"
                            :max="1"
                            :step="0.05"
                            :format-tooltip="formatCreativityTooltip"
                            style="--el-slider-main-bg-color: #47fff1"
                          />
                        </el-col>
                        <el-col :span="5">
                          <div class="video-slider-title text-right">
                            创意相关性
                          </div>
                        </el-col>
                      </el-row>
                    </div>
                    <div class="param-line" style="padding-top: 10px">
                      <el-form-item label="采样调度">
                        <template #default>
                          <div class="form-item-inner">
                            <el-select
                              v-model="params.scheduler"
                              style="width: 176px"
                            >
                              <el-option
                                v-for="item in schedulers"
                                :label="item"
                                :value="item"
                                :key="item"
                              />
                            </el-select>
                            <el-tooltip
                              effect="light"
                              content="推荐自动或者 Karras"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="图片尺寸">
                        <template #default>
                          <div class="form-item-inner">
                            <el-row :gutter="20">
                              <el-col :span="12">
                                <el-input
                                  v-model.number="params.width"
                                  placeholder="图片宽度"
                                />
                              </el-col>
                              <el-col :span="12">
                                <el-input
                                  v-model.number="params.height"
                                  placeholder="图片高度"
                                />
                              </el-col>
                            </el-row>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="迭代步数">
                        <template #default>
                          <div class="form-item-inner">
                            <el-input v-model.number="params.steps" />
                            <el-tooltip
                              effect="light"
                              content="值越大则代表细节越多，同时也意味着出图速度越慢"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="引导系数">
                        <template #default>
                          <div class="form-item-inner">
                            <el-input v-model.number="params.cfg_scale" />
                            <el-tooltip
                              effect="light"
                              content="提示词引导系数，图像在多大程度上服从提示词<br/> 较低值会产生更有创意的结果"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="随机因子">
                        <template #default>
                          <div class="form-item-inner">
                            <el-input v-model.number="params.seed" />
                            <el-tooltip
                              effect="light"
                              content="随机数种子，相同的种子会得到相同的结果<br/> 设置为 -1 则每次随机生成种子"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>

                            <el-tooltip
                              effect="light"
                              content="使用随机数"
                              raw-content
                              placement="right"
                            >
                              <el-icon
                                @click="params.seed = -1"
                                class="info-icon"
                              >
                                <Orange />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="高清修复">
                        <template #default>
                          <div class="form-item-inner">
                            <el-switch
                              v-model="params.hd_fix"
                              style="--el-switch-on-color: #47fff1"
                              size="large"
                            />
                            <el-tooltip
                              effect="light"
                              content="先以较小的分辨率生成图像，接着方法图像<br />然后在不更改构图的情况下再修改细节"
                              raw-content
                              placement="right"
                            >
                              <el-icon style="margin-left: 10px; top: 12px">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div v-show="params.hd_fix">
                      <div class="param-line">
                        <el-form-item label="重绘幅度">
                          <template #default>
                            <div class="form-item-inner">
                              <el-slider
                                v-model.number="params.hd_redraw_rate"
                                :max="1"
                                :step="0.1"
                                style="
                                  width: 180px;
                                  --el-slider-main-bg-color: #47fff1;
                                "
                              />
                              <el-tooltip
                                effect="light"
                                content="决定算法对图像内容的影响程度<br />较大的值将得到越有创意的图像"
                                raw-content
                                placement="right"
                              >
                                <el-icon class="info-icon">
                                  <InfoFilled />
                                </el-icon>
                              </el-tooltip>
                            </div>
                          </template>
                        </el-form-item>
                      </div>

                      <div class="param-line">
                        <el-form-item label="放大算法">
                          <template #default>
                            <div class="form-item-inner">
                              <el-select
                                v-model="params.hd_scale_alg"
                                style="width: 176px"
                              >
                                <el-option
                                  v-for="item in scaleAlg"
                                  :label="item"
                                  :value="item"
                                  :key="item"
                                />
                              </el-select>
                              <el-tooltip
                                effect="light"
                                content="高清修复放大算法，主流算法有Latent和ESRGAN_4x"
                                raw-content
                                placement="right"
                              >
                                <el-icon class="info-icon">
                                  <InfoFilled />
                                </el-icon>
                              </el-tooltip>
                            </div>
                          </template>
                        </el-form-item>
                      </div>

                      <div class="param-line">
                        <el-form-item label="放大倍数">
                          <template #default>
                            <div class="form-item-inner">
                              <el-input v-model.number="params.hd_scale" />
                              <el-tooltip
                                effect="light"
                                content="随机数种子，相同的种子会得到相同的结果<br/> 设置为 -1 则每次随机生成种子"
                                raw-content
                                placement="right"
                              >
                                <el-icon class="info-icon">
                                  <InfoFilled />
                                </el-icon>
                              </el-tooltip>
                            </div>
                          </template>
                        </el-form-item>
                      </div>

                      <div class="param-line">
                        <el-form-item label="迭代步数">
                          <template #default>
                            <div class="form-item-inner">
                              <el-input v-model.number="params.hd_steps" />
                              <el-tooltip
                                effect="light"
                                content="重绘迭代步数，如果设置为0，则设置跟原图相同的迭代步数"
                                raw-content
                                placement="right"
                              >
                                <el-icon class="info-icon">
                                  <InfoFilled />
                                </el-icon>
                              </el-tooltip>
                            </div>
                          </template>
                        </el-form-item>
                      </div>
                    </div>
                  </div>
                  <div class="video-box">
                    <div class="param-line pt">
                      <div class="title">
                        <img
                          class="title-icon"
                          src="/images/video/unnecessary.svg"
                        />
                        <span class="title-text"
                          >不希望呈现的内容（非必填）：</span
                        >
                      </div>
                    </div>

                    <div class="param-line">
                      <el-input
                        v-model="params.neg_prompt"
                        :autosize="{ minRows: 6, maxRows: 10 }"
                        type="textarea"
                        placeholder="写下你不希望在视频中呈现的内容。例如：动画、模糊、变形、毁容、低质量、拼贴、粒状、标志、抽象、插图、计算机生成、扭曲 …"
                      />
                    </div>
                  </div>
                </el-tab-pane>
                <el-tab-pane label="图生视频" name="img2video">
                  <template #label>
                    <span class="tabs-label">
                      <el-icon>
                        <Picture />
                      </el-icon>
                      <span>图生视频</span>
                    </span>
                  </template>
                  <div class="param-line" style="padding-top: 10px">
                    <el-form-item label="图片">
                      <template #default>
                        <el-upload
                          v-model:file-list="params.fileList"
                          :limit="1"
                          action="#"
                          list-type="picture-card"
                          :on-preview="handlePictureCardPreview"
                          :on-remove="handleRemove"
                          :on-exceed="handleExceed"
                          accept=".jpg,.png,.jepg"
                          :on-change="onChange"
                          :auto-upload="false"
                        >
                          <el-icon><Plus /></el-icon>
                        </el-upload>
                      </template>
                    </el-form-item>
                    <el-form-item label="采样方法">
                      <template #default>
                        <div class="form-item-inner">
                          <el-select
                            v-model="params.sampler"
                            style="width: 176px"
                          >
                            <el-option
                              v-for="item in samplers"
                              :label="item"
                              :value="item"
                              :key="item"
                            />
                          </el-select>
                          <el-tooltip
                            effect="light"
                            content="出图效果比较好的一般是 Euler 和 DPM 系列算法"
                            raw-content
                            placement="right"
                          >
                            <el-icon class="info-icon">
                              <InfoFilled />
                            </el-icon>
                          </el-tooltip>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div class="param-line" style="padding-top: 10px">
                    <el-form-item label="采样调度">
                      <template #default>
                        <div class="form-item-inner">
                          <el-select
                            v-model="params.scheduler"
                            style="width: 176px"
                          >
                            <el-option
                              v-for="item in schedulers"
                              :label="item"
                              :value="item"
                              :key="item"
                            />
                          </el-select>
                          <el-tooltip
                            effect="light"
                            content="推荐自动或者 Karras"
                            raw-content
                            placement="right"
                          >
                            <el-icon class="info-icon">
                              <InfoFilled />
                            </el-icon>
                          </el-tooltip>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div class="param-line">
                    <el-form-item label="图片尺寸">
                      <template #default>
                        <div class="form-item-inner">
                          <el-row :gutter="20">
                            <el-col :span="12">
                              <el-input
                                v-model.number="params.width"
                                placeholder="图片宽度"
                              />
                            </el-col>
                            <el-col :span="12">
                              <el-input
                                v-model.number="params.height"
                                placeholder="图片高度"
                              />
                            </el-col>
                          </el-row>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div class="param-line">
                    <el-form-item label="迭代步数">
                      <template #default>
                        <div class="form-item-inner">
                          <el-input v-model.number="params.steps" />
                          <el-tooltip
                            effect="light"
                            content="值越大则代表细节越多，同时也意味着出图速度越慢"
                            raw-content
                            placement="right"
                          >
                            <el-icon class="info-icon">
                              <InfoFilled />
                            </el-icon>
                          </el-tooltip>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div class="param-line">
                    <el-form-item label="引导系数">
                      <template #default>
                        <div class="form-item-inner">
                          <el-input v-model.number="params.cfg_scale" />
                          <el-tooltip
                            effect="light"
                            content="提示词引导系数，图像在多大程度上服从提示词<br/> 较低值会产生更有创意的结果"
                            raw-content
                            placement="right"
                          >
                            <el-icon class="info-icon">
                              <InfoFilled />
                            </el-icon>
                          </el-tooltip>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div class="param-line">
                    <el-form-item label="随机因子">
                      <template #default>
                        <div class="form-item-inner">
                          <el-input v-model.number="params.seed" />
                          <el-tooltip
                            effect="light"
                            content="随机数种子，相同的种子会得到相同的结果<br/> 设置为 -1 则每次随机生成种子"
                            raw-content
                            placement="right"
                          >
                            <el-icon class="info-icon">
                              <InfoFilled />
                            </el-icon>
                          </el-tooltip>

                          <el-tooltip
                            effect="light"
                            content="使用随机数"
                            raw-content
                            placement="right"
                          >
                            <el-icon
                              @click="params.seed = -1"
                              class="info-icon"
                            >
                              <Orange />
                            </el-icon>
                          </el-tooltip>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div class="param-line">
                    <el-form-item label="高清修复">
                      <template #default>
                        <div class="form-item-inner">
                          <el-switch
                            v-model="params.hd_fix"
                            style="--el-switch-on-color: #47fff1"
                            size="large"
                          />
                          <el-tooltip
                            effect="light"
                            content="先以较小的分辨率生成图像，接着方法图像<br />然后在不更改构图的情况下再修改细节"
                            raw-content
                            placement="right"
                          >
                            <el-icon style="margin-left: 10px; top: 12px">
                              <InfoFilled />
                            </el-icon>
                          </el-tooltip>
                        </div>
                      </template>
                    </el-form-item>
                  </div>

                  <div v-show="params.hd_fix">
                    <div class="param-line">
                      <el-form-item label="重绘幅度">
                        <template #default>
                          <div class="form-item-inner">
                            <el-slider
                              v-model.number="params.hd_redraw_rate"
                              :max="1"
                              :step="0.1"
                              style="
                                width: 180px;
                                --el-slider-main-bg-color: #47fff1;
                              "
                            />
                            <el-tooltip
                              effect="light"
                              content="决定算法对图像内容的影响程度<br />较大的值将得到越有创意的图像"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="放大算法">
                        <template #default>
                          <div class="form-item-inner">
                            <el-select
                              v-model="params.hd_scale_alg"
                              style="width: 176px"
                            >
                              <el-option
                                v-for="item in scaleAlg"
                                :label="item"
                                :value="item"
                                :key="item"
                              />
                            </el-select>
                            <el-tooltip
                              effect="light"
                              content="高清修复放大算法，主流算法有Latent和ESRGAN_4x"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="放大倍数">
                        <template #default>
                          <div class="form-item-inner">
                            <el-input v-model.number="params.hd_scale" />
                            <el-tooltip
                              effect="light"
                              content="随机数种子，相同的种子会得到相同的结果<br/> 设置为 -1 则每次随机生成种子"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>

                    <div class="param-line">
                      <el-form-item label="迭代步数">
                        <template #default>
                          <div class="form-item-inner">
                            <el-input v-model.number="params.hd_steps" />
                            <el-tooltip
                              effect="light"
                              content="重绘迭代步数，如果设置为0，则设置跟原图相同的迭代步数"
                              raw-content
                              placement="right"
                            >
                              <el-icon class="info-icon">
                                <InfoFilled />
                              </el-icon>
                            </el-tooltip>
                          </div>
                        </template>
                      </el-form-item>
                    </div>
                  </div>

                  <div class="param-line">
                    <el-input
                      v-model="params.prompt"
                      :autosize="{ minRows: 4, maxRows: 6 }"
                      type="textarea"
                      ref="promptRef"
                      placeholder="请在此输入绘画提示词，系统会自动翻译中文提示词，高手请直接输入英文提示词"
                    />
                  </div>

                  <div class="param-line pt">
                    <span>反向提示词：</span>
                    <el-tooltip
                      effect="light"
                      content="不希望出现的元素，下面给了默认的起手式"
                      placement="right"
                    >
                      <el-icon class="info-icon">
                        <InfoFilled />
                      </el-icon>
                    </el-tooltip>
                  </div>
                  <div class="param-line">
                    <el-input
                      v-model="params.neg_prompt"
                      :autosize="{ minRows: 4, maxRows: 6 }"
                      type="textarea"
                      placeholder="反向提示词"
                    />
                  </div>
                </el-tab-pane>
              </el-tabs>
              <div class="text-info">
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-tag>单次绘图消耗{{ klingPower }}算力</el-tag>
                  </el-col>
                  <el-col :span="12">
                    <el-tag type="success">当前可用{{ power }}算力</el-tag>
                  </el-col>
                </el-row>
              </div>
            </el-form>
          </div>
          <div class="submit-btn">
            <el-tooltip :visible="tooltipVisivle&&!params.prompt">
              <template #content>
                <span>请输入创意描述</span>
              </template>
              <el-button
                color="#47fff1"
                :dark="false"
                @mouseenter="tooltipVisivle = true"
                @mouseleave="tooltipVisivle = false"
                round
                @click="generate"
                :disabled="!params.prompt"
                >立即生成</el-button
              >
            </el-tooltip>
          </div>
        </div>
        <div class="flex-1 p-10 flex-h flex-center">
          <video-player
            ref="videoPlayerRef"
            :src="videoSrc"
            :poster="poster"
            :options="playerOptions"
            :autoplay="false"
          />
        </div>
        <div class="task-list-box">
          <div
            class="task-list-inner"
            :style="{ height: listBoxHeight + 'px' }"
          >
            <div class="job-list-box">
              <h2>任务列表</h2>
              <task-list :list="runningJobs" />

              <h2>创作记录</h2>
              <div class="finish-job-list">
                <div v-if="finishedJobs.length > 0">
                  <v3-waterfall
                    id="waterfall"
                    :list="finishedJobs"
                    srcKey="img_thumb"
                    :gap="20"
                    :bottomGap="-10"
                    :colWidth="colWidth"
                    :distanceToScroll="100"
                    :isLoading="loading"
                    :isOver="isOver"
                    @scrollReachBottom="fetchFinishJobs()"
                  >
                    <template #default="slotProp">
                      <div
                        class="job-item animate"
                        @click="showTask(slotProp.item)"
                      >
                        <el-image
                          :src="slotProp.item['img_thumb']"
                          fit="cover"
                          loading="lazy"
                        />
                        <div class="remove">
                          <el-button
                            type="danger"
                            :icon="Delete"
                            @click="removeImage($event, slotProp.item)"
                            circle
                          />
                          <el-button
                            type="warning"
                            v-if="slotProp.item.publish"
                            @click="publishImage($event, slotProp.item, false)"
                            circle
                          >
                            <i class="iconfont icon-cancel-share"></i>
                          </el-button>
                          <el-button
                            type="success"
                            v-else
                            @click="publishImage($event, slotProp.item, true)"
                            circle
                          >
                            <i class="iconfont icon-share-bold"></i>
                          </el-button>
                        </div>
                      </div>
                    </template>

                    <template #footer>
                      <div class="no-more-data">
                        <span>没有更多数据了</span>
                        <i class="iconfont icon-face"></i>
                      </div>
                    </template>
                  </v3-waterfall>
                </div>
                <el-empty :image-size="100" v-else />
              </div>
              <!-- end finish job list-->
            </div>
          </div>
        </div>
        <!-- end task list box -->
      </div>

      <!-- 任务详情弹框 -->
      <el-dialog
        v-model="showTaskDialog"
        title="绘画任务详情"
        :fullscreen="true"
      >
        <el-row :gutter="20">
          <el-col :span="16">
            <div
              class="img-container"
              :style="{ maxHeight: fullImgHeight + 'px' }"
            >
              <el-image :src="item['img_url']" fit="contain" />
            </div>
          </el-col>
          <el-col :span="8">
            <div class="task-info">
              <div class="info-line">
                <el-divider> 正向提示词 </el-divider>
                <div class="prompt">
                  <span>{{ item.prompt }}</span>
                  <el-icon
                    class="copy-prompt-sd"
                    :data-clipboard-text="item.prompt"
                  >
                    <DocumentCopy />
                  </el-icon>
                </div>
              </div>

              <div class="info-line">
                <el-divider> 反向提示词 </el-divider>
                <div class="prompt">
                  <span>{{ item.params.neg_prompt }}</span>
                  <el-icon
                    class="copy-prompt-sd"
                    :data-clipboard-text="item.params.neg_prompt"
                  >
                    <DocumentCopy />
                  </el-icon>
                </div>
              </div>

              <div class="info-line">
                <div class="wrapper">
                  <label>采样方法：</label>
                  <div class="item-value">{{ item.params.sampler }}</div>
                </div>
              </div>

              <div class="info-line">
                <div class="wrapper">
                  <label>图片尺寸：</label>
                  <div class="item-value">
                    {{ item.params.width }} x {{ item.params.height }}
                  </div>
                </div>
              </div>

              <div class="info-line">
                <div class="wrapper">
                  <label>迭代步数：</label>
                  <div class="item-value">{{ item.params.steps }}</div>
                </div>
              </div>

              <div class="info-line">
                <div class="wrapper">
                  <label>引导系数：</label>
                  <div class="item-value">{{ item.params.cfg_scale }}</div>
                </div>
              </div>

              <div class="info-line">
                <div class="wrapper">
                  <label>随机因子：</label>
                  <div class="item-value">{{ item.params.seed }}</div>
                </div>
              </div>

              <div v-if="item.params.hd_fix">
                <el-divider> 高清修复 </el-divider>
                <div class="info-line">
                  <div class="wrapper">
                    <label>重绘幅度：</label>
                    <div class="item-value">
                      {{ item.params.hd_redraw_rate }}
                    </div>
                  </div>
                </div>

                <div class="info-line">
                  <div class="wrapper">
                    <label>放大算法：</label>
                    <div class="item-value">{{ item.params.hd_scale_alg }}</div>
                  </div>
                </div>

                <div class="info-line">
                  <div class="wrapper">
                    <label>放大倍数：</label>
                    <div class="item-value">{{ item.params.hd_scale }}</div>
                  </div>
                </div>

                <div class="info-line">
                  <div class="wrapper">
                    <label>迭代步数：</label>
                    <div class="item-value">{{ item.params.hd_steps }}</div>
                  </div>
                </div>
              </div>

              <div class="copy-params">
                <el-button type="primary" round @click="copyParams(item)"
                  >画一张同款的</el-button
                >
              </div>
            </div>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog v-model="dialogVisible">
        <img w-full :src="dialogImageUrl" alt="Preview Image" />
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { onMounted, onUnmounted, ref, watch } from "vue";
import {
  Delete,
  DocumentCopy,
  InfoFilled,
  Orange,
  Picture,
} from "@element-plus/icons-vue";
import { httpGet, httpPost } from "@/utils/http";
import { ElMessage, ElMessageBox, ElNotification } from "element-plus";
import Clipboard from "clipboard";
import {checkSession, getSystemInfo} from "@/store/cache";
import { useRouter } from "vue-router";
import { getSessionId } from "@/store/session";
import { useSharedStore } from "@/store/sharedata";
import TaskList from "@/components/TaskList.vue";

const listBoxHeight = ref(0);
// const paramBoxHeight = ref(0)
const fullImgHeight = ref(window.innerHeight - 60);
const showTaskDialog = ref(false);
const item = ref({});
const isLogin = ref(false);
const loading = ref(true);
const colWidth = ref(220);
const tooltipVisivle = ref(false)
const store = useSharedStore();
const activeName = ref("text2video");
const resizeElement = function () {
  listBoxHeight.value = window.innerHeight - 80;
  // paramBoxHeight.value = window.innerHeight - 200
};
resizeElement();
window.onresize = () => {
  resizeElement();
};
const samplers = [
  "Euler a",
  "DPM++ 2S a",
  "DPM++ 2M",
  "DPM++ SDE",
  "DPM++ 2M SDE",
  "UniPC",
  "Restart",
];
const schedulers = ["Automatic", "Karras", "Exponential", "Uniform"];
const scaleAlg = ["Latent", "ESRGAN_4x", "R-ESRGAN 4x+", "SwinIR_4x", "LDSR"];
const params = ref({
  width: 1024,
  height: 1024,
  sampler: samplers[0],
  scheduler: schedulers[0],
  seed: -1,
  steps: 20,
  cfg_scale: 7,
  hd_fix: false,
  hd_redraw_rate: 0.7,
  hd_scale: 2,
  hd_scale_alg: scaleAlg[0],
  hd_steps: 0,
  prompt: "",
  neg_prompt: "",
  fileList: [],
});
const dialogImageUrl = ref("");
//视频播放器
const videoPlayerRef = ref();

const dialogVisible = ref(false);

// 视频标题
const title = ref("");
title.value = "小学生写作业";
//视频封面
const poster = ref("https://images.jlntv.cc/ai/logo-2.png");
// 视频链接地址
const videoSrc = ref("http://localhost:8888/files/aa.mp4");

// 视频播放器配置
let playerOptions = ref({
  height: 1100,
  // width: document.documentElement.clientWidth, //播放器宽度
  playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
  muted: false, // 默认情况下将会消除任何音频。
  loop: false, // 导致视频一结束就重新开始。
  preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
  language: "zh-CN",
  aspectRatio: "16:9", // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
  fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
  notSupportedMessage: "此视频暂无法播放，请稍后再试", // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
  controls: true,
  controlBar: {
    timeDivider: true,
    durationDisplay: true,
    remainingTimeDisplay: false,
    fullscreenToggle: true, // 全屏按钮
  },
});
watch(
  () => params.fileList,
  async (newValue) => {
    console.log("newValue :>> ", newValue);
    var aa = document.querySelector(".el-upload--picture-card");
    if (newValue.length) {
      aa.style.display = "none";
    } else {
      setTimeout(() => {
        aa.style.display = "inline-flex";
      }, 1100);
    }
  }
);
const onChange = (uploadFile, uploadFiles) => {
  console.log("uploadFile :>> ", uploadFile);
  const reader = new FileReader();
  reader.readAsDataURL(uploadFile.raw);
  reader.onload = (e) => {
    // 后端入参 转base64  在提交的时候
    // const response = await uploadImgToBase64(file.raw)
    //       return response.result.replace(/.*;base64,/, '') //
  };
};
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};
const handleExceed = (files, uploadFiles) => {
  ElMessage.warning(`只能上传一张图片`);
};
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url;
  dialogVisible.value = true;
};
const runningJobs = ref([]);
const finishedJobs = ref([]);
const router = useRouter();
// 检查是否有画同款的参数
const _params = router.currentRoute.value.params["copyParams"];
if (_params) {
  params.value = JSON.parse(_params);
}
const power = ref(0);
const klingPower = ref(0); // 生成一个视频消耗算力

const socket = ref(null);
const userId = ref(0);
const heartbeatHandle = ref(null);
const connect = () => {
  let host = process.env.VUE_APP_WS_HOST;
  if (host === "") {
    if (location.protocol === "https:") {
      host = "wss://" + location.host;
    } else {
      host = "ws://" + location.host;
    }
  }

  // 心跳函数
  const sendHeartbeat = () => {
    clearTimeout(heartbeatHandle.value);
    new Promise((resolve, reject) => {
      if (socket.value !== null) {
        socket.value.send(
          JSON.stringify({ type: "heartbeat", content: "ping" })
        );
      }
      resolve("success");
    }).then(() => {
      heartbeatHandle.value = setTimeout(() => sendHeartbeat(), 5000);
    });
  };

  const _socket = new WebSocket(
    host + `/api/sd/client?user_id=${userId.value}`
  );
  _socket.addEventListener("open", () => {
    socket.value = _socket;

    // 发送心跳消息
    sendHeartbeat();
  });

  _socket.addEventListener("message", (event) => {
    if (event.data instanceof Blob) {
      const reader = new FileReader();
      reader.readAsText(event.data, "UTF-8");
      reader.onload = () => {
        const message = String(reader.result);
        if (message === "FINISH") {
          page.value = 0;
          isOver.value = false;
          fetchFinishJobs();
        }
        fetchRunningJobs();
      };
    }
  });

  _socket.addEventListener("close", () => {
    if (socket.value !== null) {
      connect();
    }
  });
};

const clipboard = ref(null);
onMounted(() => {
  initData();
  clipboard.value = new Clipboard(".copy-prompt-sd");
  clipboard.value.on("success", () => {
    ElMessage.success("复制成功！");
  });

  clipboard.value.on("error", () => {
    ElMessage.error("复制失败！");
  });

  httpGet("/api/config/get?key=system")
    .then((res) => {
      klingPower.value = res.data.kling_video_power;
      console.log(res.data.kling_video_power);
      // params.value.neg_prompt = res.data.sd_neg_prompt
    })
    .catch((e) => {
      ElMessage.error("获取系统配置失败：" + e.message);
    });
});

onUnmounted(() => {
  clipboard.value.destroy();
  if (socket.value !== null) {
    socket.value.close();
    socket.value = null;
  }
});

const initData = () => {
  checkSession()
    .then((user) => {
      power.value = user["power"];
      userId.value = user.id;
      isLogin.value = true;
      page.value = 0;
      fetchRunningJobs();
      fetchFinishJobs();
      connect();
    })
    .catch(() => {});
};

const fetchRunningJobs = () => {
  if (!isLogin.value) {
    return;
  }

  // 获取运行中的任务
  httpGet(`/api/sd/jobs?finish=0`)
    .then((res) => {
      const jobs = res.data;
      const _jobs = [];
      for (let i = 0; i < jobs.length; i++) {
        if (jobs[i].progress === -1) {
          ElNotification({
            title: "任务执行失败",
            dangerouslyUseHTMLString: true,
            message: `任务ID：${jobs[i]["task_id"]}<br />原因：${jobs[i]["err_msg"]}`,
            type: "error",
          });
          power.value += sdPower.value;
          continue;
        }
        _jobs.push(jobs[i]);
      }
      runningJobs.value = _jobs;
    })
    .catch((e) => {
      ElMessage.error("获取任务失败：" + e.message);
    });
};

const page = ref(0);
const pageSize = ref(20);
const isOver = ref(false);
// 获取已完成的任务
const fetchFinishJobs = () => {
  if (!isLogin.value || isOver.value === true) {
    return;
  }
  loading.value = true;
  page.value = page.value + 1;

  httpGet(
    `/api/sd/jobs?finish=1&page=${page.value}&page_size=${pageSize.value}`
  )
    .then((res) => {
      if (res.data.length < pageSize.value) {
        isOver.value = true;
      }
      const imageList = res.data;
      for (let i = 0; i < imageList.length; i++) {
        imageList[i]["img_thumb"] =
          imageList[i]["img_url"] + "?imageView2/4/w/300/h/0/q/75";
      }
      if (page.value === 1) {
        finishedJobs.value = imageList;
      } else {
        finishedJobs.value = finishedJobs.value.concat(imageList);
      }

      loading.value = false;
    })
    .catch((e) => {
      ElMessage.error("获取任务失败：" + e.message);
    });
};

// 创建绘图任务
const promptRef = ref(null);
const generate = () => {
  if (params.value.prompt === "") {
    promptRef.value.focus();
    return ElMessage.error("请输入绘画提示词！");
  }

  if (!isLogin.value) {
    store.setShowLoginDialog(true);
    return;
  }

  if (params.value.seed === "") {
    params.value.seed = -1;
  }
  params.value.session_id = getSessionId();
  httpPost("/api/sd/image", params.value)
    .then(() => {
      ElMessage.success("绘画任务推送成功，请耐心等待任务执行...");
      power.value -= sdPower.value;
    })
    .catch((e) => {
      ElMessage.error("任务推送失败：" + e.message);
    });
};

const showTask = (row) => {
  item.value = row;
  showTaskDialog.value = true;
};

const copyParams = (row) => {
  params.value = row.params;
  showTaskDialog.value = false;
};

const removeImage = (event, item) => {
  event.stopPropagation();
  ElMessageBox.confirm("此操作将会删除任务和图片，继续操作码?", "删除提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      httpGet("/api/sd/remove", { id: item.id, user_id: item.user })
        .then(() => {
          ElMessage.success("任务删除成功");
          page.value = 0;
          isOver.value = false;
          fetchFinishJobs();
        })
        .catch((e) => {
          ElMessage.error("任务删除失败：" + e.message);
        });
    })
    .catch(() => {});
};

// 发布图片到作品墙
const publishImage = (event, item, action) => {
  event.stopPropagation();
  let text = "图片发布";
  if (action === false) {
    text = "取消发布";
  }
  httpGet("/api/sd/publish", {
    id: item.id,
    action: action,
    user_id: item.user,
  })
    .then(() => {
      ElMessage.success(text + "成功");
      item.publish = action;
      page.value = 0;
      isOver.value = false;
      fetchFinishJobs();
    })
    .catch((e) => {
      ElMessage.error(text + "失败：" + e.message);
    });
};
const formatCreativityTooltip = (val) => {
  if (val > 0.7) {
    return val + " (数值较大)，有小概率出现图片和描述内容冲突导致的画面错误";
  }
  return val;
};
</script>

<style lang="stylus">
@import "@/assets/css/video-kling.styl"
@import "@/assets/css/custom-scroll.styl"
</style>
